﻿var search;

function searchClick(sender) {

    $("#output").hide("normal", keywordExecute);
}

function keywordExecute() {
    $("#output").empty();
    var keywords = $("#keywords").attr("value");
    search = new Search();
    search.startAt = parseInt($("#start").attr("value"));
    search.resultCount = parseInt($("#count").attr("value"));
    search.searchType = "Keyword";
    search.setKeywordQuery();
    search.searchText = keywords;
    search.contextUri = $("#url").attr("value");
    search.displayFields = $("#fields").attr("value");

    var callback = ProcessBasic;
    if (search.displayFields != null && search.displayFields.length > 0) {
        callback = ProcessAdvanced;
    }
    search.execute(callback);
}

function fullTextSearchClick(sender) {

    $("#output").hide("normal", fullTextExecute);
}

function fullTextExecute() {
    $("#output").empty();
    var query = $("#queryText").attr("value");
    search = new Search();
    search.startAt = parseInt($("#start").attr("value"));
    search.resultCount = parseInt($("#count").attr("value"));
    search.setFullTextQuery();
    search.searchText = query;
    search.contextUri = $("#url").attr("value");

    var callback = ProcessAdvanced;
    search.execute(callback);
}

function ShowStatus(results) {

    infoDiv = $("<div/>");
    infoDiv.attr("class", "info");

    $("Status", results).each(function() {
        newDiv = $("<div/>");
        newDiv.append("<strong>" + this.text + "</strong>");
        infoDiv.append(newDiv);
    });

    $("DebugErrorMessage", results).each(function() {
        newDiv = $("<div/>");
        newDiv.append(this.text);
        infoDiv.append(newDiv);
    });

    $("#output").append(infoDiv);
    

}

function ProcessBasic(results) {
    ShowStatus(results);
    $("Document", results).each(function() {
        var title = $(this).find("Title").text();
        var description = $(this).find("Description").text();
        var date = $(this).find("Date").text();
        var link = $(this).find("LinkUrl").text();

        anchorP = $("<p/>");
        descP = $("<p/>");
        dateP = $("<p/>");
        newAnchor = $("<a/>").attr("href", "blah");
        newAnchor.append(title);
        anchorP.append(newAnchor);
        newDiv = $("<div/>");
        newDiv.attr("class", "result");
        newDiv.append(newAnchor);
        descP.append(description);
        dateP.append(date);
        newDiv.append(anchorP);
        newDiv.append(descP);
        newDiv.append(dateP);
        $("#output").append(newDiv);
    });
    $("#output").show("normal");
}

function ProcessAdvanced(results) {
    ShowStatus(results);
    $("Document", results).each(function() {
        WriteResult(this);
    });
    $("#output").show("normal");
}

function WriteResult(doc) {
    var newDiv = $("<div/>");
    newDiv.attr("class", "result");
    var newTable = $("<table/>");

    $("Property", doc).each(function() {
        var name = $("Name", this).text();
        var val = $("Value", this).text();
        newTable.append('<tr><td valign="top">' + name + ':</td><td valign="top">' + val + '</td></tr>');
    });

    newDiv.append(newTable);
    $("#output").append(newDiv);
}
